﻿using DataTurnClient.Interface;
using DataTurnClient.Model;
using ReactiveUI;
using Splat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading.Tasks;

namespace DataTurnClient.ViewModel
{
    public class RouteViewModel : ReactiveObject
    {
        public ViewModelActivator Activator { get; }

        public IScheduler MainScheduler;

        #region 属性
        public RouteLine RouteLine
        {
            get => _RouteLine;
            set => this.RaiseAndSetIfChanged(ref _RouteLine, value);
        }
        private RouteLine _RouteLine = new RouteLine();

        public List<RouteLine> RouteList
        {
            get => _RouteList;
            set => this.RaiseAndSetIfChanged(ref _RouteList, value);
        }
        private List<RouteLine> _RouteList = new List<RouteLine>();

        #endregion


        #region Command

        public ReactiveCommand<Unit, Unit> SelectLineCommand { get; }
        public ReactiveCommand<Unit, Result<int>> CreateLineCommand { get; }
        public ReactiveCommand<RouteLine, Result<int>> DeleteLineCommand { get; }

        #endregion
        public RouteViewModel()
        {
            SelectLineCommand = ReactiveCommand.Create(SelectLineCommandImpl);
            CreateLineCommand = ReactiveCommand.Create(CreateLineCommandImpl);
            DeleteLineCommand = ReactiveCommand.Create<RouteLine ,Result<int>>(DeleteLineCommandImpl);
        }

        private void SelectLineCommandImpl()
        {
            var service = Locator.Current.GetService<IDBHelperService>();
            using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
            {
                var DbJobList = db.Queryable<RouteLine>().ToList();
                Globals.RouteList = DbJobList;
                if (DbJobList != null && DbJobList.Count > 0)
                {
                    RouteList.Clear();
                    RouteList = DbJobList;
                }
            }
        }
        private Result<int> CreateLineCommandImpl()
        {
            Result<int> result = new Result<int>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    result.Data = db.Insertable(RouteLine).ExecuteCommand();
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"CreateLineCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }
        private Result<int> DeleteLineCommandImpl(RouteLine line)
        {
            Result<int> result = new Result<int>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    result.Data = db.Deleteable<RouteLine>().Where(x => x.Id == line.Id).ExecuteCommand();
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"DeleteLineCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }
    }
}
